<template>
    <div>
      <el-breadcrumb separator="/" style="padding-left:10px;padding-bottom:10px;font-size:12px;">
        <el-breadcrumb-item :to="{ path: '/main' }">首页</el-breadcrumb-item>
        <el-breadcrumb-item>房屋管理</el-breadcrumb-item>
        <el-breadcrumb-item>房屋信息</el-breadcrumb-item>
      </el-breadcrumb>
      <!--用户列表卡片-->
      <el-card class="box-card card" id="card">
        <el-form :inline="true" :model="searchForm" class="demo-form-inline">
          <el-form-item size="mini" label="状态" label-width="70px" style="font-weight: bold">
            <el-select clearable v-model="searchForm.state" placeholder="请选择" :style="{width: '100%'}">
              <el-option label="已上架" :value="0"></el-option>
              <el-option label="已出租" :value="1"></el-option>
              <el-option label="已过期" :value="2"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item size="mini" label="房屋类型" label-width="70px" style="font-weight: bold">
            <el-select clearable v-model="searchForm.houseType" placeholder="请选择" :style="{width: '100%'}">
              <el-option label="商品房" :value="0"></el-option>
              <el-option label="单身公寓住宅" :value="1"></el-option>
              <el-option label="单元式住宅" :value="2"></el-option>
              <el-option label="复式住宅" :value="3"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item size="mini" label="租期" label-width="70px" style="font-weight: bold">
            <el-select clearable v-model="searchForm.leaseTerm" placeholder="请选择" :style="{width: '100%'}">
              <el-option label="三月" :value="90"></el-option>
              <el-option label="半年" :value="180"></el-option>
              <el-option label="一年" :value="360"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item size="mini" label="装修情况" label-width="70px" style="font-weight: bold">
            <el-select clearable v-model="searchForm.decorateState" placeholder="请选择" :style="{width: '100%'}">
              <el-option label="精美装修" :value="0"></el-option>
              <el-option label="简单装修" :value="1"></el-option>
              <el-option label="毛坯装修" :value="2"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item size="mini" style="margin-left: 10px;">
            <el-button icon="el-icon-refresh" @click="resetForm">重置</el-button>
            <el-button icon="el-icon-search" @click="onSubmit(1)" type="primary">查询</el-button>
            <el-button icon="el-icon-plus" @click="show" type="success">添加</el-button>
          </el-form-item>
        </el-form>
        <!--表格内容显示区域-->
        <el-table
          :max-height="this.getTableMaxHeight()"
          v-loading="loading" element-loading-text="数据加载中..."
          :data="page.records"
          border
          size="mini"
          style="width: 100%;">
          <el-table-column
            width="60px"
            align="center"
            type="index"
            label="序号">
          </el-table-column>
          <el-table-column
            width="200px"
            align="center"
            prop="address"
            label="所在地">
            <template slot-scope="scope">
              <span class="el-tag el-tag--primary el-tag--mini el-tag--light">{{ scope.row.address }}</span>
            </template>
          </el-table-column>
          <el-table-column
            width="150px"
            align="center"
            prop="addressInfo"
            label="详细地址">
          </el-table-column>
          <el-table-column
            width="80px"
            align="center"
            prop="state"
            label="状态">
            <template slot-scope="scope">
              <el-tag type="primary" v-if="scope.row.state === 0">已上架</el-tag>
              <el-tag type="success" v-else-if="scope.row.state === 1">已出租</el-tag>
              <el-tag type="info" v-else-if="scope.row.state === 2">已过期</el-tag>
            </template>
          </el-table-column>
          <el-table-column
            width="130px"
            align="center"
            prop="verifyState"
            label="租赁状态">
            <template slot-scope="scope">
              <el-tag type="danger" v-if="scope.row.verifyState === 1">审核不过</el-tag>
              <el-tag type="success" v-else-if="scope.row.verifyState === 2">审核通过</el-tag>
            </template>
          </el-table-column>
          <el-table-column
            width="150px"
            align="center"
            prop="errorInfo"
            label="审核反馈">
            <template slot-scope="scope">
              <el-tag type="info" v-if="scope.row.verifyState === 0">没出结果</el-tag>
              <el-tag type="danger" v-else-if="scope.row.verifyState === 1">{{scope.row.errorInfo}}</el-tag>
              <el-tag type="success" v-else-if="scope.row.verifyState === 2">可以发布</el-tag>
            </template>
          </el-table-column>
          <el-table-column
            width="100px"
            align="center"
            prop="avatar"
            label="图片">
            <template slot-scope="scope">
<!--              <el-popover placement="right" trigger="hover">-->
<!--                <img :src="scope.row.avatar" style="height: 200px;width: 200px" alt="alt"/>-->
<!--                <img slot="reference" :src="scope.row.avatar" alt="alt" style="height: 50px;width: 50px;cursor: pointer">-->
<!--              </el-popover>-->
              <video :src="scope.row.avatar" style="height: 50px;width: 50px" ></video>
            </template>
          </el-table-column>
          <el-table-column
            width="70px"
            align="center"
            prop="price"
            label="租金(元)">
          </el-table-column>
          <el-table-column
            width="120px"
            align="center"
            prop="houseType"
            label="房屋类型">
            <template slot-scope="scope">
              <el-tag type="warning" v-if="scope.row.houseType === 0">商品房</el-tag>
              <el-tag type="warning" v-else-if="scope.row.houseType === 1">公寓式住宅</el-tag>
              <el-tag type="warning" v-else-if="scope.row.houseType === 2">单元式住宅</el-tag>
              <el-tag type="warning" v-else-if="scope.row.houseType === 3">复式住宅</el-tag>
            </template>
          </el-table-column>
          <el-table-column
            width="70px"
            align="center"
            prop="area"
            label="面积㎡">
          </el-table-column>
          <el-table-column
            width="100px"
            align="center"
            prop="decorateState"
            label="装修情况">
            <template slot-scope="scope">
              <el-tag type="warning" v-if="scope.row.decorateState === 0">精美装修</el-tag>
              <el-tag type="warning" v-else-if="scope.row.decorateState === 1">简单装修</el-tag>
              <el-tag type="warning" v-else-if="scope.row.decorateState === 2">毛坯装修</el-tag>
            </template>
          </el-table-column>
          <el-table-column
            width="70px"
            align="center"
            prop="leaseTerm"
            label="租期">
            <template slot-scope="scope">
              <el-tag v-if="scope.row.leaseTerm === 90">三月</el-tag>
              <el-tag v-else-if="scope.row.leaseTerm === 180">半年</el-tag>
              <el-tag v-else-if="scope.row.leaseTerm === 360">一年</el-tag>
            </template>
          </el-table-column>
          <el-table-column
            align="center"
            label="操作">
            <template slot-scope="scope">
              <el-tooltip effect="dark" content="编辑房屋" placement="top" :enterable="false">
                <el-button @click="show(scope.row.id, true)" size="mini" type="primary" icon="el-icon-edit"
                           circle></el-button>
              </el-tooltip>
              <el-tooltip effect="dark" content="删除房屋" placement="top" :enterable="false">
                <el-button @click="deleteAccount(scope.row.id)" size="mini" type="danger" icon="el-icon-delete"
                           circle></el-button>
              </el-tooltip>
              <el-tooltip effect="dark" content="设置房屋已过期" placement="top" :enterable="false">
                <el-button size="mini" type="info" icon="el-icon-s-release"
                           circle></el-button>
              </el-tooltip>
            </template>
          </el-table-column>
        </el-table>
        <!--分页-->
        <el-pagination
          v-if="page.total > 0"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="page.current"
          :page-sizes="[5, 10, 20, 30]"
          :page-size="page.size"
          layout="total, sizes, prev, pager, next, jumper"
          :total="page.total">
        </el-pagination>
      </el-card>
      <!--用户添加/修改模态框 :userData="isEdit === true ? userData : null"-->
      <AddOrUpdate :addOrUpdateVisible="addOrUpdateVisible"
                   :isEdit="isEdit"
                   :id="id"
                   @dataUpdate="init"
                   @changeShow="showAddOrUpdate">
      </AddOrUpdate>
    </div>
</template>

<script>
  import { HouseInfoByUserId,deleteHouseInfo,searchHouseInfoListPage } from '@/api/houseInfo'
  import AddOrUpdate from './AddOrUpdate'

  export default {
    name: 'HouseInfo',
    data () {
      return {
        searchForm: {
          state: '',
          houseType: '',
          leaseTerm: '',
          decorateState: ''
        },
        page: {
          records: [],
          total: 0,
          size: 0,
          current: 0,
          orders: []
        },
        pageNum: 1,
        pageSize: this.getPageSize(),
        // 控制新增编辑弹窗的显示与隐藏
        addOrUpdateVisible: false,
        isEdit: false,
        id: 0,
        loading: false,
      }
    },
    components: {
      AddOrUpdate
    },
    created () {
      this.init()
    },
    methods: {
      init () {
        const _this = this
        _this.loading = true
        /* 创建组件的时候加载页面信息 */
        HouseInfoByUserId(_this.pageNum, _this.pageSize).then(response => {
          _this.page = response.data
        }).finally(() => {
          _this.loading = false
        })
      },

      onSubmit (page = this.pageNum) {
        const _this = this
        searchHouseInfoListPage(this.searchForm, page, this.pageSize).then(response => {
          _this.page = response.data
        })
      },
      handleSizeChange (val) {
        /* 将value赋值给size, 重新查询数据 */
        this.pageSize = val
        this.onSubmit()
      },
      handleCurrentChange (val) {
        this.pageNum = val
        this.onSubmit()
      },
      resetForm () {
        this.searchForm.state = ''
        this.searchForm.houseType = ''
        this.searchForm.decorateState = ''
        this.searchForm.leaseTerm = ''
        this.pageNum = 1
        this.pageSize = this.getPageSize()
        const _this = this
        HouseInfoByUserId(this.pageNum, this.pageSize).then(response => {
          _this.page = response.data
        })
      },
      // 按钮点击事件 显示新增编辑弹窗组件
      show (id, edit) {
        this.addOrUpdateVisible = true
        if (edit) {
          // 如果是编辑框，将绑定的数据传递给子组件
          this.id = id
        }
        this.isEdit = edit
      },
      // 监听 子组件弹窗关闭后触发，有子组件调用
      showAddOrUpdate (data) {
        this.addOrUpdateVisible = data !== 'false'
      },
      deleteAccount (id) {
        this.$confirm('是否删除该房屋信息?', '警告', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          const _this = this
          deleteHouseInfo(id).then(response => {
            _this.init()
            _this.RightMessage(_this, '成功', response.message, 'success')
          })
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除房屋'
          })
        })
      }
    },
    mounted () {
      // 获取屏幕高度
      document.getElementById('card').style.height = this.getWindowHeight()
    }
  }
</script>

<style scoped>

</style>
